package com.atguigu.yygh.user.controller;


import com.atguigu.common.result.Result;
import com.atguigu.utils.JwtHelper;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**挂号网站使用
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xueyoupeng
 * @since 2023-06-12
 */
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Autowired
    UserInfoService userInfoService;

    /*
     * 手机号+短信验证码  == 登录
     * @author: XueYouPeng
     * @time: 2023/6/12 20:59
     */
     @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
        //要用到name 和 token
         Map<String,Object> map = userInfoService.login(loginVo);
         return Result.ok().data(map);
    }

    /**
     * 用户认证中心页面，查询当前用户信息
     * 需求：前端需要在请求头中传递token令牌
     *       token=xxxx.xx.xx
     *
     *       如何从请求头中获取一个值
     * @return
     */
     @GetMapping("getUserInfo")
     public Result getUserInfo(HttpServletRequest request){
         String jwtToken = request.getHeader("token");
         if (StringUtils.isEmpty(jwtToken)){
             return Result.error().message("令牌为空，请先登录");
         }
         UserInfo userInfo = userInfoService.getUserInfo(request);
         return Result.ok().data("userInfo",userInfo);
     }

     @PostMapping("userauth")
     public Result userauth(
             @RequestBody UserAuthVo userAuthVo,
             HttpServletRequest request
     ){
         //从request中获取请求头 拿到token
         String token = request.getHeader("token");
         //判断token是否存在
         if (StringUtils.isEmpty(token)){
             return Result.error().message("令牌不存在");
         }
         Long userId = JwtHelper.getUserId(token);
         userInfoService.userauth(userId,userAuthVo);
         return Result.ok();
     }
}























